/*
 * Copyright (C) 2016-2019 Samuel Wall
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package uk.co.samuelwall.materialtaptargetprompt.sample;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.ListContainer;
import uk.co.samuelwall.materialtaptargetprompt.MaterialTapTargetPrompt;
import uk.co.samuelwall.materialtaptargetprompt.extras.focals.RectanglePromptFocal;
import uk.co.samuelwall.materialtaptargetprompt.sample.adapter.ListAdapter;
import uk.co.samuelwall.materialtaptargetprompt.sample.utils.NumUtil;
import uk.co.samuelwall.materialtaptargetprompt.sample.weight.MaterButton.ButtonFloat;

/**
 * ListAbility
 *
 * @since 2021-06-21
 */
public class ListAbility extends Ability {
    final String[] values = new String[]{"Banana", "Apple", "Pear"};

    ListContainer listView;
    ListAdapter mAdapter;
    int itemIndex = 0;
    private DependentLayout mRootView;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_list);
        initViews();
    }

    private void initViews() {
        findComponentById(ResourceTable.Id_ig_back).setClickedListener(component -> terminateAbility());
        listView = (ListContainer) findComponentById(ResourceTable.Id_list);
        mRootView = (DependentLayout) findComponentById(ResourceTable.Id_list_dl);
        mAdapter = new ListAdapter(values);
        listView.setItemProvider(mAdapter);
        listView.setLongClickable(false);
        ButtonFloat fab = (ButtonFloat) findComponentById(ResourceTable.Id_fab);
        fab.setClickedListener(view -> showPrompt());
        getMainTaskDispatcher().delayDispatch(this::showPrompt, NumUtil.INT_1000);
    }

    private void showPrompt() {
        new MaterialTapTargetPrompt.Builder(ListAbility.this, mRootView)
                .setTarget(mAdapter.getItemView(itemIndex++))
                .setPrimaryText("List item")
                .setSecondaryText("This is targeting a list item")
                .setPromptFocal(new RectanglePromptFocal(ListAbility.this))
                .show();
        if (itemIndex == values.length) {
            itemIndex = 0;
        }
    }
}
